package xxy.com.yangproject.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import androidx.annotation.Nullable;

import android.os.Build;
import android.text.StaticLayout;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * Created by xxy on 2018/8/4.
 *
 */

public class MyTextView extends View {

    private Paint paint;
    private int width;
    private int height;
    private Path path;
    private StaticLayout staticLayout;
    private String text1 = "你好啊";
    private float runAdvance;
    private Rect bounds;

    public MyTextView(Context context) {
        super(context);
        init();
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //paint 设置
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#666666"));
        paint.setTextSize(52);
        paint.setShadowLayer(8, 0 , 0 , Color.parseColor("#E57373"));
        paint.setStyle(Paint.Style.FILL);

        //bounds
        bounds = new Rect();

        //屏幕的宽高
        DisplayMetrics dm = getResources().getDisplayMetrics();
//        height = dm.heightPixels;
//        width = dm.widthPixels;
        height = 800;
        width = 1000;

        //设置路径
        path = new Path();
        path.addCircle(width / 2, height / 2, 90, Path.Direction.CW);

        //得到光标的起始位置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            runAdvance = paint.getRunAdvance(text1, 0, text1.length(), 0, text1.length(), true, text1.length());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //带阴影的文字
        canvas.drawText(text1, width / 2 - 60, height / 2, paint);
        //得到紧紧包裹文字的边框的信息
        paint.getTextBounds(text1, 0, text1.length(), bounds);
        //绘制光标
        canvas.drawLine(width / 2 - 60 + runAdvance + 10, height / 2 - bounds.bottom + bounds.top , width / 2 - 60 + runAdvance + 10, height / 2, paint);
        //画带路径的文字
        canvas.drawTextOnPath("勇敢 自信 乐观 正直 善良", path, 0, -200, paint);

    }
}
